package com.maigao.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 * @program: maigao
 * @description:
 * @author: yongza
 * @create: 2021-09-04 18:09
 **/
public class DataUtil {

    /**
     * 判断数字是否在某个区间, 包含临界值
     *
     * @param data
     * @param min
     * @param max
     * @return
     */
    public static boolean isBetween(Integer data, Integer min, Integer max) {
        return data >= min && data <= max;
    }

    /**
     * 计算两个数的乘法, 结果保留一位小数
     *
     * @param d1
     * @param d2
     * @return
     */
    public static double multiply(double d1, double d2) {
        BigDecimal a1 = new BigDecimal(Double.toString(d1));
        BigDecimal b1 = new BigDecimal(Double.toString(d2));
        BigDecimal result = a1.multiply(b1);// 相乘结果
        BigDecimal one = new BigDecimal("1");
        double value = result.divide(one, 2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return value;
    }

    /**
     * 保留两位小数(四舍五入)
     *
     * @param d
     * @return
     */
    public static String formatTwoPoint(double d) {
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(d);
    }

    /**
     * 保留n位小数, 后面的舍去
     *
     * @param data
     * @param bit
     * @return
     */
    public static double formatData(double data, int bit) {
        DecimalFormat df;
        switch (bit) {
            case 1:
                df = new DecimalFormat("0.0");
                df.setRoundingMode(RoundingMode.DOWN);
                return Double.parseDouble(df.format(data));
            case 2:
                df = new DecimalFormat("0.00");
                df.setRoundingMode(RoundingMode.DOWN);
                return Double.parseDouble(df.format(data));
            case 3:
                df = new DecimalFormat("0.000");
                df.setRoundingMode(RoundingMode.DOWN);
                return Double.parseDouble(df.format(data));
            case 4:
                df = new DecimalFormat("0.0000");
                df.setRoundingMode(RoundingMode.DOWN);
                return Double.parseDouble(df.format(data));
            case 5:
                df = new DecimalFormat("0.00000");
                df.setRoundingMode(RoundingMode.DOWN);
                return Double.parseDouble(df.format(data));
            default:
                df = new DecimalFormat("0.00");
                df.setRoundingMode(RoundingMode.DOWN);
                return Double.parseDouble(df.format(data));
        }
    }

    /**
     * 当浮点型数据位数超过10位之后，数据变成科学计数法显示。用此方法可以使其正常显示。
     *
     * @param value
     * @return Sting
     */
    public static String formatFloatNumber(double value) {
        if (value != 0.00) {
            DecimalFormat df = new DecimalFormat("0.0000");
            return df.format(value);
        } else {
            return "0.0000";
        }

    }

    public static String formatFloatNumber(Double value) {
        if (value != null) {
            if (value.doubleValue() != 0.00) {
                DecimalFormat df = new DecimalFormat("0.0000");
                return df.format(value.doubleValue());
            } else {
                return "0.0000";
            }
        }
        return "";
    }

    public static void main(String[] args) {
        double d = 0.0054;

    }
}
